package com.wltt.healing.healinguserweb.router;

import com.wltt.healing.healinguserweb.handler.UserHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;

import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * @Author: wltt
 * @Date: 2021/9/8 15:26：
 * @Version: 1.0
 * @Description: TODO
 */
@Configuration
public class UserRouter {

    private static final Logger log = LogManager.getLogger(UserRouter.class);

    @Resource
    private ReactiveRedisTemplate<String, String> reactiveRedisTemplate;

    @Bean
    public RouterFunction<ServerResponse> auth(UserHandler userHandler) {
        return route()
                .path(
                        "/user",
                        b1 -> b1
                                .nest(accept(MediaType.ALL), b2 -> b2
                                        .POST("/auth/securityKey", userHandler::securityKey)
                                        .POST("/auth/login", userHandler::login)
                                )
                                //拦截器(生成固定的sessionId)
                                .filter((request, next) -> request.session()
                                        .flatMap(webSession -> {
                                            Map<String, Object> attr = webSession.getAttributes();
                                            attr.put("", "");
                                            return next.handle(request);
                                        }))

                )
                .build();
    }

//    @Bean
//    public RouterFunction<ServerResponse> routerHello(UserHandler userHandler) {
//        return route()
//                .path(
//                        "/user",
//                        b1 -> b1
//                                .nest(accept(MediaType.ALL), b2 -> b2
//                                        .GET("/auth/securityKey", userHandler::securityKey)
//                                        .GET("/auth/login", userHandler::login)
//                                )
//                                //拦截器
//                                .filter((request, next) -> {
//                                    System.out.println("我是外部filter1");
//                                    return next.handle(request);
//                                })
//                )
//                .build();
//    }
}
